package controller

import (
	"fx_swift_boss/api"
	"fx_swift_boss/internal/service"
	"fx_swift_boss/pkg/code"
	"fx_swift_boss/pkg/internalerrors"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 新增手续费配置
func CreateMerchantFee(c *gin.Context) {
	req := &api.FeeReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.(internalerrors.ServiceError).Msg)
		return
	}

	result, err := service.NewFeeSrv().CreateMerchantFee(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 查询手续费详情
func QueryMerchantFeeDetails(c *gin.Context) {
	req := &api.FeeDetailsReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.(internalerrors.ServiceError).Msg)
		return
	}

	result, err := service.NewFeeSrv().QueryMerchantFeeDetails(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 手续费审批下发
func DoFeeApproval(c *gin.Context) {
	id := c.Query("id")
	status := c.Query("status")

	err := service.NewFeeSrv().DoFeeApproval(c, id, status)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 查询上级手续费详情
func GetParentFeeDetails(c *gin.Context) {
	agentAccount := c.Query("agentAccount")
	feeType := c.Query("feeType")

	result, err := service.NewFeeSrv().GetParentFeeDetails(c, agentAccount, feeType)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}
